package ua.kharkov.knure.pereverziev.diploma.web.command;

import java.io.IOException;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ua.kharkov.knure.pereverziev.diploma.db.DAOServices.ServiceFactory;
import ua.kharkov.knure.pereverziev.diploma.db.entity.Ticket;
import ua.kharkov.knure.pereverziev.diploma.db.entity.User;

public class ShowOrdersCommand implements Command {

	private final Logger log = LoggerFactory.getLogger(ShowOrdersCommand.class);

	@Override
	public String execute(final HttpServletRequest request,
			final HttpServletResponse response) throws IOException,
			ServletException {
		final String forward = "/WEB-INF/jsp/personalOrders.jsp";

		final HttpSession session = request.getSession();

		final User user = (User) session.getAttribute("user");

		final Set<Ticket> tickets = ServiceFactory.getInstance()
				.getTicketService().getUserTicketsSet(user);

		log.debug("Found tickets: " + tickets.size());

		session.setAttribute("tickets", tickets);

		session.setAttribute("lastPage", forward);
		return forward;
	}

}
